package org.ccay.core.request;

import org.ccay.core.ioc.Ccay;
import org.ccay.core.log.CcayLoggerFactory;
import org.ccay.core.log.ILogger;

import org.springframework.core.env.Environment;


/**
 * 应用程序信息
 * @author chaowangbang
 * @since 2011-8-25
 */
public class Application implements IApplicationPrincipal{

	private static final ILogger log = CcayLoggerFactory.getLogger("Ccay");
	private static final long serialVersionUID = 3088642430828812087L;
	private String appName;
	private String scope; 
	private String contextPath;
	private int logsTableIndex;
	private String dbType;
	private String serviceVersion;
	private String currentProfile;
	

	public int getLogsTableIndex() {
		return logsTableIndex;
	}
	public void setLogsTableIndex(int logsTableIndex) {
		this.logsTableIndex = logsTableIndex;
	}

	private static Object lockObj = new Object();
	
	{
		initCcay();
	}
	
	/** 
	 * 初始化Ccay，打印About信息
	 * @author chaowangbang
	 * @since 2012-11-14
	 */
	private static void initCcay(){
		 String msg = "Welcom to use CCAY";
		 log.info(msg);
		 msg = null;
	}
	/**
	 * @return the network
	 */
	public String getNetwork() {
	    return network;
	}
	/**
	 * @param network the network to set
	 */
	public void setNetwork(String network) {
	    this.network = network;
	}

	private String network;
	/**
	 * @return enviorment
	 */
	public String getEnviorment() {
		if(currentProfile == null){
			Environment env = Ccay.getContext().getBean(Environment.class);
			currentProfile = env.getActiveProfiles()[0];
		}
		return currentProfile;
	}
 
	/**
	 * @return appName
	 */
	public String getAppName() {
		return appName;
	}
	/**
	 * @param appName 要设置的 appName
	 */
	public void setAppName(String appName) {
		log.info("Application Name set to :{0}",appName);
		this.appName = appName;
	}
	/**
	 * @return scope
	 */
	public String getScope() {
		return scope;
	}
	/**
	 * @param scope 要设置的 scope
	 */
	public void setScope(String scope) {
		log.info("Application Scope set to :{0}",scope);
		this.scope = scope;
	}
 
	/**
	 * @return the contextPath
	 */
	public String getContextPath() {
		return contextPath;
	}
	/**
	 * @param contextPath the contextPath to set
	 */
	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}
	
	/** IApplicationPrincipal实例 **/
	private static volatile IApplicationPrincipal appPrincipal = null;

	/**
	 * 获取当前应用的身份
	 * 
	 * @author chaowangbang
	 * @since 2012-6-26
	 * @return
	 */
	public static IApplicationPrincipal getCurrent() {
		if (appPrincipal != null) {
			return appPrincipal;
		}

		synchronized (lockObj) {
			if (appPrincipal == null) {
				appPrincipal = Ccay.getContext().getBean("applicationPrincipal", IApplicationPrincipal.class);
			}
		}
		return appPrincipal;
	}
	/**
	 * @return the dbType
	 */
	public String getDbType() {
		return dbType;
	}
	/**
	 * @param dbType the dbType to set
	 */
	public void setDbType(String dbType) {
		this.dbType = dbType;
	}
	
	public String getServiceVersion() {
		return serviceVersion;
	}
	public void setServiceVersion(String serviceVersion) {
		this.serviceVersion = serviceVersion;
	}

}
